home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6326 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: rcp6.elan.af.mil!rscernix!danpop
  2. From: danpop@mail.cern.ch (Dan Pop)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help : Printf() format %E !
  5. Date: 23 Feb 96 12:12:55 GMT
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <danpop.825077575@rscernix>
  8. References: <4g18nf$jps@ias2.ichange.com> <danpop.824495447@rscernix> <312C786F.6696@oc.com>
  9. NNTP-Posting-Host: ues5.cern.ch
  10. X-Newsreader: NN version 6.5.0 #7 (NOV)
  11.  
  12. In <312C786F.6696@oc.com> Larry Weiss <lfw@oc.com> writes:
  13.  
  14. >Dan Pop wrote:
  15. > > 
  16. > > In <4g18nf$jps@ias2.ichange.com> akmp@mindware.soft.net (anil kumar m p) writes:
  17. > > >i am involved converting programs written in Fortran onto C.
  18. > > >one problem that i am unable to solve is printing exponential
  19. > > >values in C.
  20. > > >Fortran prints exponential values with 0.4567E02
  21. > > >whereas C prints the same as 4.5670E01.
  22. > > >How do i make C print in 0.somethingEsomething format.
  23. >
  24. >> The %E format cannot do what you need, by definition.  So, you have to
  25. >> split the number into mantissa and exponent and printf them separately.
  26. >> You can do this either mathematically or by playing with stdio functions:
  27. >>   double mantissa;
  28. >>   int exponent;
  29. >>   sprintf(buff, "%E", 0.4567E02);
  30. >>   *strchr(buff, 'E') = ' ';
  31. >>   sscanf(buff, "%lf %d", &mantissa, &exponent);
  32. >>   mantissa /= 10;
  33. >>   if (mantissa != 0) exponent++;
  34. >>   else exponent = 0;
  35. >>   printf("%fE%+.02d\n", mantissa, exponent);
  36. >> 
  37. >> If mathematical functions are inexpensive on your system, splitting
  38. >> the number into mantissa and exponent with log10 and pow might be
  39. >> more efficient. 
  40. >
  41. >Or, use sprintf() to get a string containing the components, and
  42. >then use character manipulations to adjust the formatting.
  43.  
  44. Incrementing the exponent using character manipulations is not exactly
  45. straightforward (it isn't rocket science, either) :-)
  46.  
  47. Dan
  48. --
  49. Dan Pop
  50. CERN, CN Division
  51. Email: danpop@mail.cern.ch 
  52. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  53.